package csc.fresher.filter;


import java.io.IOException;

import javax.faces.application.ViewExpiredException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

public class SessionTimeoutFilter implements Filter {

    private String homePage = "login.jsf";

    @Override
    public void init(FilterConfig filter) throws ServletException {
        //Filter initialized
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            if (isSessionCheckingNeeded(httpServletRequest)) {
                if ((isInvalidSession(httpServletRequest))) {
                    String timeoutUrl = httpServletRequest.getContextPath() + "/internetbanking/" + getHomePage();
                    httpServletResponse.sendRedirect(timeoutUrl);
                    return;
                }
            }
        }

        try {
            chain.doFilter(request, response);
        } catch (ViewExpiredException ve) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;

            String timeoutUrl = httpServletRequest.getContextPath() + "/internetbanking/" + getHomePage();
            httpServletResponse.sendRedirect(timeoutUrl);

        } catch (Exception e) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;

            String timeoutUrl = httpServletRequest.getContextPath() + "/internetbanking/" + getHomePage();

            if (StringUtils.countOccurrencesOf(e.getMessage(), "could not be restored.") > 0) {
                httpServletResponse.sendRedirect(timeoutUrl);
                return;
            }

            httpServletResponse.sendRedirect(timeoutUrl);
            return;
        }
    }

    private boolean isInvalidSession(HttpServletRequest httpServletRequest) {

        boolean sessionInValid = (httpServletRequest.getRequestedSessionId() != null) 
                                 && !httpServletRequest.isRequestedSessionIdValid();
        return sessionInValid;
    }

    private boolean isSessionCheckingNeeded(HttpServletRequest request) {

        int counter = StringUtils.countOccurrencesOf(request.getRequestURL().toString(), getHomePage())
                + StringUtils.countOccurrencesOf(request.getRequestURL().toString(), getHomePage());

        if (counter > 0) {
            return false;
        }
        return true;
    }

    @Override
    public void destroy() {
        //Session destroyed
    }

    public String getHomePage() {
        return homePage;
    }

    public void setHomePage(String homePage) {
        this.homePage = homePage;
    }
}